<?php
namespace ShopApi\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class OrdersApiController extends AbstractRestfulController
{
    public function getList()
    {			
			
    }
    public function get($shop_id)
    {			
		header('Access-Control-Allow-Origin: *');	
		$orderDeliveryDetailsTable=$this->getServiceLocator()->get('Models\Model\OrderDeliveryDetailsFactory');
		$values=explode('-',$shop_id);
		$shop_Id=$values[0];
		$getShopOrders = $orderDeliveryDetailsTable->getShopOrders($shop_Id)->toArray();
		if(count($getShopOrders)!=0){
			return new JsonModel(array(
				'output' 	=> 	'success',
				'result' 	=> 	json_encode($getShopOrders),
				'value'		=>	1,
			));
		}else{
			return new JsonModel(array(
				'output' => 'fail',
				'value'		=>	0,
			));
		}	
    }
    public function create($data)
    {
		header('Access-Control-Allow-Origin: *');	
		$shop_id=$data['shop_id'];
		$orderDeliveryDetailsTable=$this->getServiceLocator()->get('Models\Model\OrderDeliveryDetailsFactory');
		$discountTable=$this->getServiceLocator()->get('Models\Model\DiscountsFactory');
		$totPendingOrders     = $orderDeliveryDetailsTable->getShopwiseReportsforOrders($shop_id,'tot_orders');
		$getShopOrdersRenewal = $orderDeliveryDetailsTable->getShopwiseReportsforOrders($shop_id,'auto_renewal');
		$totSentPriceOrders   = $orderDeliveryDetailsTable->getShopwiseReportsforOrders($shop_id,'cnt_of_sent_price');
		$totConfirmedOrders   = $orderDeliveryDetailsTable->getShopwiseReportsforOrders($shop_id,'cnt_of_cnf_by_admin');
		$totDeliveredOrders   = $orderDeliveryDetailsTable->getShopwiseReportsforOrders($shop_id,'delivered_orders');
		$totUnPaidDiscountofOrders = $discountTable->getPaidAndUnPaidDiscounts($shop_id,'un_paid');
		$totPaidDiscountofOrders   = $discountTable->getPaidAndUnPaidDiscounts($shop_id,'paid');
		if( $totPendingOrders!=0 || $getShopOrdersRenewal!=0 || $totSentPriceOrders!=0 || $totConfirmedOrders!=0 || $totDeliveredOrders!=0){
			return new JsonModel(array(
				'totOrders' 		    => $totPendingOrders,
				'totrenewalOrders' 		=> $getShopOrdersRenewal,
				'totSentorders' 		=> $totSentPriceOrders,
				'totConfirmedOrders' 	=> $totConfirmedOrders,
				'totDeliveredOrders' 	=> $totDeliveredOrders,							
				'totunPaidOrders' 	    => $totUnPaidDiscountofOrders,							
				'totPaidOrders' 	    => $totPaidDiscountofOrders,							
			));
		}else{
			return new JsonModel(array(
				'totOrders' 		    => 0,
				'totrenewalOrders' 		=> 0,
				'totSentorders' 		=> 0,
				'totConfirmedOrders' 	=> 0,
				'totDeliveredOrders' 	=> 0,							
				'totunPaidOrders' 	    => 0,							
				'totPaidOrders' 	    => 0,
			));
		}	
    }
    public function update($order_delivery_details_id, $price)
    {	
		header('Access-Control-Allow-Origin: *');
		$orderDeliveryDetailsTable=$this->getServiceLocator()->get('Models\Model\OrderDeliveryDetailsFactory');
		$notificationTable=$this->getServiceLocator()->get('Models\Model\NotificationsFactory');
		$updateOrderDetails = $orderDeliveryDetailsTable->updateOrderPrice($order_delivery_details_id,$price['price']);
		$getAllUserNotifications = $notificationTable->sendPriceToAdmin($price,'3','1');
		if($updateOrderDetails){
			return new JsonModel(array(
				'output' 	=> 'success',
				'value'		=>	1,
			));
		}else{
			return new JsonModel(array(
				'output' 	=> 'fail',
				'value'		=>	0,
			));
		}
    }
    public function delete($id)
    {
       
    }
}